/**
 * AgrregationLogWriter.java
 * ************************** 
 * @date Apr 14, 2011
 * @author Michele Tamburini
 * 
 * **************************
 */
package gossipServices.aggregation;

import java.io.FileNotFoundException;
import java.io.PrintWriter;

/**
 * Description: 
 *
 */
public class AggregationLogWriter {
	
	public final static String DEST_PATH = "log/";
	public final static String FILE_EXT = ".log";
	public final static String FILE_PATTERN_NAME = "agg";
	
	private String nodeName;
	
	private String fileName = new String("");
	
	private static PrintWriter fileWriter;
	
	public AggregationLogWriter(String nodeName){
		this.nodeName = nodeName;
		fileName = DEST_PATH + nodeName + FILE_PATTERN_NAME + FILE_EXT;
		try {
			fileWriter = new PrintWriter(fileName);
			System.out.println(this.getClass().getSimpleName()+" the new file is: "+fileName);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} 
	}
	
	public boolean writeLogLine(String line){
		boolean ret = false;
		try{
			fileWriter.println(line);
			fileWriter.flush();
			ret = true;
		}catch(Exception e){
			System.err.println("Cannot write the "+FILE_PATTERN_NAME+ " log of "+nodeName+"\n" +
					"exception is: "+e);
		}
		return ret;
	}

}
